/**
 * 主页js
 */

define(function(require, exports, module) {
	var util = require('lm-util'),
        imgScroll = require('mod-imgScroll'),
        magic = require('mod-magic'),
        $ = require('$');

	//行业资讯、交通警示、社区精选
    util.tab("mouseover",".index-left-infolist .index-tab-title", "current", ".index-left-infolist .content");
    //最新资讯
    util.tab("mouseover","#J_NewInfo .hd-tab", "selected", "#J_NewInfo .content");
    //汽车视频
    util.tab("mouseover","#J_AutoVideo .index-title-tab", "active", "#J_AutoVideo ul.video-list");
    //买车助手
    util.tab("mouseover","#J_BuyHelper .index-title-tab", "active", "#J_BuyHelper .J_BuyHelper");
    //最新推荐
    util.tab("mouseover","#J_NewRec .hd-tab", "selected", "#J_NewRec .content");


	/*----------------------------------------图片滚动------------------------------------------*/
	//交通
	var trafficImg = new imgScroll("#traffic-img-scroll", 8);
	trafficImg.slide();
	/*----------------------------------------幻灯片---------------------------------------------*/
	//头部图片轮转
	var topMagic = new magic("#js-top-rotate");
	topMagic.magic();
	topMagic.preImg();
	topMagic.nextImg();
	topMagic.indexImg();
	//行业资讯图片轮转
	var indusMagic = new magic("#js-indus-rotate");
	indusMagic.magic();
	indusMagic.nextImg();
	indusMagic.preImg();

    //车型库选择
    var Brand = require('mod-autoBrand'), autoSelect = new Brand();
    autoSelect.makeSelect();
    autoSelect.change();

    //右下角抽奖图片
    if($('.lottery-small').length>0){
        $('.lottery-small>a.close').click(function(){
            $('.lottery-small').hide();
        });
    }

    //中奖列表
    (function(){
        if($('#J_lotteryList').length<=0) return;
        var awardNum = $("#J_lotteryList>li").length,scrollTimer;
        function scrollResult(){
            $("#J_lotteryList").animate({"marginTop":"-25px"},500,function(){
                $("#J_lotteryList").css({marginTop:0}).find("li:first").appendTo($("#J_lotteryList"));
            });
        }
        if(awardNum > 5){
            $("#J_lotteryList").hover(function(){
                clearInterval(scrollTimer);
            },function(){
                scrollTimer = setInterval(function(){
                    scrollResult();
                },3000);
            }).trigger("mouseout")
        }
    })();

    //添加收藏
    util.addFavorite($("#tip-fav"));
    //关闭
    $("#tip-close").click(function(){
        var OPTION ={ expires: 7};
        util.cookie("ixdcw-tip",'1',OPTION);
        $("#fav-tip").slideUp();
    });
    //加载
    if(!util.cookie("ixdcw-tip")){
        setTimeout(function(){
            $("#fav-tip").slideDown();
        },1000)
    }

    //车主服务
    function slidePic(id){
        util.slide({
            "list":$(id),
            "elemClass":".index-pics",
            "left":$(id).parents(".prelative").children(".icon-pre-gray"),
            "right":$(id).parents(".prelative").children(".icon-next-gray"),
            "width":960,
            "timer":5000,
            "speed":1000
        });
    }
    slidePic("#J_SlideOwner");//车主服务

    //最新图库
    function scrollTab(opt){
        var timerID,
            _this = opt.id,
            _left = opt.left,
            _right = opt.right,
            _tab = opt.tab,
            moveWidth = opt.width,
            timer = opt.timer ? opt.timer : 5000,
            index = 0,
            speed = opt.speed ? parseInt(opt.speed, 10) : 500,
            len = _tab.find("li").length;

        //滚动
        function scrollAct(index){
            _left.unbind("click", scrollLeft); //滑动时先取消点击滑动事件
            _right.unbind("click", scrollRight);
            _tab.find("li").eq(index).addClass("active").siblings("li").removeClass("active")
            _this.animate({
                marginLeft : -moveWidth*index
            }, speed, function () {
                _left.bind("click", scrollLeft); //滑动结束后绑定按钮的点击事件
                _right.bind("click", scrollRight);
            });
        }
        //向左滚动函数
        function scrollLeft() {
            index--;
            if(index<0){index=len-1;}
            scrollAct(index)
        }
        //向右滚动函数
        function scrollRight() {
            index++;
            if(index>=len){index=0;}
            scrollAct(index)
        }
        //自动播放
        function autoPlay() {
            if ( timer ) timerID = window.setInterval(scrollRight, timer);
        }
        //停止自动播放
        function autoStop() {
            if ( timer ) window.clearInterval(timerID);
        }

        //鼠标事件绑定
        _this.hover(autoStop, autoPlay).trigger("mouseout"); //加载完成后自动开始
        _left.click(scrollLeft).hover(autoStop, autoPlay)//向左鼠标事件绑定
        _right.click(scrollRight).hover(autoStop, autoPlay)//向右鼠标事件绑定
        _tab.find("li").hover(function(){
            autoStop();
            var curIndex = $(this).index();
            scrollAct(curIndex);
        },function(){
            autoPlay()
        });
    }
    scrollTab({
        id:$("#J_SlideNewPic"),
        left:$("div#J_SlideNewPic").parents("div.prelative").children("a.icon-pre-gray"),
        right:$("div#J_SlideNewPic").parents("div.prelative").children("a.icon-next-gray"),
        tab:$("div#J_SlideNewPic").parents("div.prelative").siblings("div.index-title").children("ul.index-title-tab"),
        width:"960"
    });
});